#include "mq_c_connection.hpp"

int main()
{
    //1. 实例化异步工作线程对象
    bitmq::AsyncWorker::ptr awp = std::make_shared<bitmq::AsyncWorker>();
    //2. 实例化连接对象
    bitmq::MyConnection::ptr conn = std::make_shared<bitmq::MyConnection>("127.0.0.1", 8085, awp);
    if(conn.get()!= nullptr){
        ELOG("客户端连接成功!");
    }
    //3. 通过连接创建信道
    bitmq::ClientChannel::ptr channel = conn->openChannel();
    if( channel.get() != nullptr){
        DLOG("打开信道成功");
    }
    //4. 通过信道提供的服务完成所需
     //1. 声明一个交换机exchange1, 交换机类型为广播模式
    google::protobuf::Map<std::string, std::string> tmp_map;
    bool ret = channel->declareExchange("exchange1", bitmq::ExchangeType::TOPIC, true, false, tmp_map);
    if(ret == true){
        DLOG("交换机 %s 创建成功!","excnange1");
    }

    //  2. 声明一个队列queue1
    ret = channel->declareQueue("queue1", true, false, false, tmp_map);
    if(ret == true){
        DLOG("队列 %s 创建成功!","queue1");
    }
    //  3. 声明一个队列queue2
    ret = channel->declareQueue("queue2", true, false, false, tmp_map);
    if(ret == true){
        DLOG("队列 %s 创建成功!","queue2");
    }
    //  4. 绑定queue1-exchange1，且binding_key设置为queue1
    ret = channel->queueBind("exchange1", "queue1", "queue1");
    if(ret == true){
        DLOG("绑定 %s 创建成功!","1");
    }
    //  5. 绑定queue2-exchange1，且binding_key设置为news.music.#
    ret = channel->queueBind("exchange1", "queue2", "news.music.#");
    if(ret == true){
        DLOG("绑定 %s 创建成功!","2");
    }

    //5. 循环向交换机发布消息
    std::cout<<"开始发送消息"<<std::endl;
    for (int i = 0; i < 10; i++) {
        bitmq::BasicProperties bp;//消息的属性
        bp.set_id(bitmq::UUIDHelper::uuid());
        bp.set_deliver_mode(bitmq::DeliverMode::DURABLE);//可持久化
        //设置消息的routing_key，广播模式下随意，主题和直接模式下需要用到
        bp.set_routing_key("news.music.pop");
        channel->basicPublish("exchange1",  "Hello World-" + std::to_string(i),&bp);
    }
    std::cout<<"发送消息结束"<<std::endl;
    bitmq::BasicProperties bp;
    bp.set_id(bitmq::UUIDHelper::uuid());
    bp.set_deliver_mode(bitmq::DeliverMode::DURABLE);
    bp.set_routing_key("news.music.sport");
    channel->basicPublish("exchange1", "Hello Bite",&bp);

    
    //6. 关闭信道
    conn->closeChannel(channel);
    awp->pool.stop();
    return 0;
}